package com.print.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.print.dao.UserDAO;
import com.print.domain.User;

@Controller
public class UserController {

	private UserDAO userDAO;
	
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	@RequestMapping(value={"addUser.htm"}, method=RequestMethod.POST)
	public @ResponseBody String addUser(HttpServletRequest request, @RequestParam String username, @RequestParam String password) {
		User toSave = new User();
		toSave.setAdmin(false);
		toSave.setPassword(password);
		toSave.setUsername(username);
		
		HttpSession session = request.getSession();
		if (session.getAttribute("user") != null && (Boolean)session.getAttribute("privileges")) {
			toSave.setAdmin(true);
			userDAO.saveUser(toSave);
			return "success";
		}
		if(session.getAttribute("user") == null) {
			toSave.setAdmin(false);
			this.userDAO.saveUser(toSave);
			return "success";
		}
		return "error";
	}
	
	@RequestMapping(value={"updateUser.htm"}, method=RequestMethod.POST)
	public @ResponseBody String updateInfo(HttpServletRequest request, @RequestParam String username, @RequestParam String vecchiaPassword, @RequestParam String password) {
		HttpSession session = request.getSession();
		if(this.userDAO.getUser(session.getAttribute("user").toString(), vecchiaPassword) == null)
			return "error";
		User toSave = this.userDAO.getUser(session.getAttribute("user").toString());
		toSave.setPassword(password);
		toSave.setUsername(username);
	
		if (session.getAttribute("user") != null && (Boolean)session.getAttribute("privileges")) {
			toSave.setAdmin(true);
			userDAO.updateUser(toSave);
			session.setAttribute("user", username);
			return "success";
		}
		toSave.setAdmin(false);
		this.userDAO.updateUser(toSave);
		session.setAttribute("user", username);
		return "success";
	}
	
	@RequestMapping("listUsers")
	public String listUsers(Map<String, Object> map) {
		map.put("usersList", userDAO.listUser());
		map.put("user", new User());
		return "listUsers.htm";
	}
	
	@RequestMapping("deleteUser.htm")
	public String deleteUser(HttpServletRequest request) {
		HttpSession session = request.getSession();
		userDAO.deleteUser(session.getAttribute("user").toString());
		session.invalidate();
		session = null;
		return "index";
	}
	
	@RequestMapping("logout.htm")
	public ModelAndView logout(HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		session.invalidate();
		session = null;
		
		return new ModelAndView("loginPage");
	}
	
	@RequestMapping(value={"login.htm"}, method=RequestMethod.POST)
	public @ResponseBody String login(HttpServletRequest request, Map<String, Object> map, @RequestParam String username, @RequestParam String password) {
		HttpSession session = request.getSession();
		
		try {
			User logged = userDAO.getUser(username, password);
			if (logged == null) {
				return "error";
			} else {
				session.setAttribute("user", username);
				session.setAttribute("privileges", logged.getAdmin());
				if(logged.getAdmin())
					return "welcomeAdmin";
				return "welcomeUser";
			}
		} catch (Exception e) {
			return "error" + e.getMessage();
		}
	}
	
	@RequestMapping(value={"checkUsername.htm"}, method = RequestMethod.POST)
	public @ResponseBody String checkUsername(HttpServletRequest request, @RequestParam String username) {
		HttpSession session = request.getSession();
		/*
		 * Se si verifica questa condizione vuol dire che sto modificando i miei dati e che non voglio cambiare username
		 */
		if(session.getAttribute("user") != null && session.getAttribute("user").toString().equals(username))
			return "yes";
		
		User user = this.userDAO.getUser(username);
		
		if(user == null)
			return "yes";
		return "no";
	}
	
	@RequestMapping("isLogged.htm")
	public @ResponseBody String isLogged(HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session.getAttribute("user") == null) {
			return "false";
		}
		return "true";
	}
	
	@RequestMapping("getUsername.htm")
	public @ResponseBody String getUsername(HttpServletRequest request) {
		HttpSession session = request.getSession();
		return session.getAttribute("user").toString();
	}

	@RequestMapping(value = { "gestioneUtenti.htm" }, method = RequestMethod.POST)
	public @ResponseBody String gestioneUtenti() {
		List<User> users = userDAO.listNotAdmin();
		String result = "";
		for (User user : users) {
			result += user.getUsername() + "#";
		}
		System.out.println(result);
		return result;
	}

	@RequestMapping(value = { "eliminaUser.htm" }, method = RequestMethod.POST)
	public @ResponseBody String eliminaUser(@RequestParam String usernames) {
		String[] nomi = usernames.split("#");
		String s = "";
		for (String string : nomi) {
			if (!userDAO.deleteUser(string))
				return "no";
			else
				s += string + "#";
		}
		return s;
	}
}